package cn.kinoko.config.minio;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;

/**
 * 定义MinIO的参数属性类
 * @author kinoko
 */
@Data
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {
    /**
     * 账户名称
     */
    private String accessKey;
    /**
     * 账户密码
     */
    private String secretKey;
    /**
     * MinIO连接地址
     */
    private String endpoint;
    /**
     * 桶名称
     */
    private String bucket;
    /**
     * 访问文件的地址
     */
    private String readPath;
    /**
     * 可上传的文件后缀名
     */
    private List<String> fileExt;
    /**
     * 缩略图压缩率
     */
    private Double scale;

    public static final String THUMB_PREFIX = "thumb/";
    public static final String BLANK_PREFIX = "blank/";

    public String getThumbPathPrefix() {
        return readPath + "/" + bucket + "/"+ THUMB_PREFIX;
    }

    public String getFilePathPrefix() {
        return readPath + "/" + bucket + "/";
    }

    public String path2Key(String pathUrl) {
        // 1.切割掉域名 http://xxx.cn/oss/bucketName/filePath -> /bucketName/filePath
        return pathUrl.replaceFirst(readPath + "/", "")
                // 2.切割掉bucketName /bucketName/filePath -> /filePath
                .replaceFirst(bucket + "/", "");
    }
}